package sharewithus.analyzer.utils;


/**
 * 自定义邻接表
 * 
 * @author terry
 * 
 */
public class AdjList<T> {
	private DeLinkedList<Edge<T>>[] adjTable; // 邻接表
	public int nVerts;// 顶点数

	/**
	 * 
	 * @param vNum 顶点数
	 */
	@SuppressWarnings("unchecked")
	public AdjList(int vNum) {
		nVerts = vNum;
		adjTable = new DeLinkedList[vNum];
		for (int i = 0; i < vNum; i++)
			adjTable[i] = new DeLinkedList<Edge<T>>();
	}

	/**
	 * 添加边
	 * @param src 原点
	 * @param dest 目标
	 * @param t 数据
	 */
	public void addEdge(int src,int dest,T t) {
		adjTable[src].add(new Edge<T>(src, dest, t));
	}

	/**
	 * 获取顶点上的所有边
	 * 
	 * @param vertex
	 * @return
	 */
	public DeLinkedList<Edge<T>> getEdges(int vertex) {
		DeLinkedList<Edge<T>> ll = adjTable[vertex];
		if (ll == null)
			return null;
		return ll;
	}

	/**
	 * 有向边
	 * @author terry
	 *
	 * @param <T>
	 */
	public static class Edge<T>{
		int src;//原点
		int dest;//目标
		T data;//数据
		
		public Edge(int src, int dest, T d) {
			this.src = src;
			this.dest = dest;
			this.data = d;
		}
		
		public String toString(){
			return data.toString();
		}

		public T getData() {
			return data;
		}

		public void setData(T data) {
			this.data = data;
		}
		
		
	}
	
	/**
	 * Feel free to make this prettier, if you'd like
	 */
	public String toString() {
		StringBuilder temp = new StringBuilder();

		for (int index = 0; index < nVerts; index++) {
			if (adjTable[index] == null) {
				continue;
			}
			temp.append("node:");
			temp.append(index);
			temp.append(": ");
			temp.append(adjTable[index].toString());
			temp.append("\n");
		}

		return temp.toString();
	}
}
